﻿function CreateParentInputCheckBox(webPartId) {
    return $("<th nowrap scope='col' class='ms-vh2'></th>").append(
                $("<input type='checkbox' title='(de)select all items' />").attr("id", webPartId + "0")
                .click(function() {
                    var checked = $(this).attr("checked");
                    $("[id^=" + webPartId + "_]").attr("checked", checked);
                })
            );
}

function CreateChildInputCheckBox(webPartId, itemId) {
    return $("<td></td>").append(
                $("<input type='checkbox' />").attr("id", webPartId + "_" + itemId)
                                              .val(itemId)
                                              .click(function() {
                                                  $("#" + webPartId + "0").attr("checked", $(this).attr("checked") && $("[id^=" + webPartId + "_]:not(:checked)").length == 0);
                                              })
            );
}

function AddCheckBoxesToListView(webPartId) {
    $("#" + webPartId + " table.ms-listviewtable>tbody")
            .find(">tr.ms-viewheadertr").prepend(CreateParentInputCheckBox(webPartId)).end()
            .find(">tr:not(.ms-viewheadertr)")
                .each(function() {
                    var itemId = $(this).find("td.ms-vb-title>table[id]").attr("id");
                    if (itemId) {
                        $(this).prepend(CreateChildInputCheckBox(webPartId, itemId));
                    }
                });
}

function IsSelectable(webPartId) {
    var selectableItems = $("#" + webPartId + " table.ms-listviewtable>tbody>tr:not(.ms-viewheadertr)>td.ms-vb-title>table[id]").length;
    return selectableItems > 0;
}

function RemoveCheckBoxesFromListView(webPartId) {
    $("[id^=" + webPartId + "_], #" + webPartId + "0").parent().remove();
}

function GetSelectedItemsString(webPartId) {
    var selectedIds = new Array();
    $("[id^=" + webPartId + "_]:checked")
        .each(function() {
                selectedIds.push($(this).val());
        });

    return selectedIds.join(",");
}

function ListItemSelection_ButtonClick(senderId, webPartId) {
    //jQueryon mozilla does not work with namespaces. We have to work with plain old javascript here...
    var sender = document.getElementById(senderId);

    if (sender.getAttribute("remove")) {
        RemoveCheckBoxesFromListView(webPartId);
        sender.setAttribute("text" ,"Enable item selection");
        sender.setAttribute("description", "Enable the selection of items.");
        sender.removeAttribute("remove");
    } else {
        AddCheckBoxesToListView(webPartId)
        sender.setAttribute("text", "Disable item selection")
        sender.setAttribute("description", "Disable the selection of items.");
        sender.setAttribute("remove", true);
    }
}

function ListItemSelection_Init(senderId, webPartId) {

    var sender = document.getElementById(senderId);
    if (!IsSelectable(webPartId)) {
        sender.parentNode.removeChild(sender);
    }
    if(sender.text == 'Enable item selection')
    {
        AddCheckBoxesToListView(webPartId);
        sender.setAttribute("text", "Disable item selection")
        sender.setAttribute("description", "Disable the selection of items.");
        sender.setAttribute("remove", true);
    }
}

function CreateContext(webPartId) {
    var ctxName = $("#" + webPartId + " table[CTXName]:first").attr("CTXName");
    var createCtx = new Function("setupMenuContext(" + ctxName + ");");
    createCtx();
}

function ListItemSelection_ConfirmAction(webPartId, action) {
    CreateContext(webPartId);
    
    var message = "Are you sure you want to " + action + " the selected item(s)?";
    
    return confirm(message);
}